package net.chromedpork.scraper.configuration;



import java.util.Map;

import org.w3c.dom.Document;

import net.chromedpork.hypertext.validation.Assertion;



/**
 *
 *  Configuration object factory
 *
 *  @author Inode <inode@chromedpork.net>
 *
 */

public class ConfigurationFactory
{
    /**
     *
     *  Fabricates a scraper configuration object from an XML document.
     *
     *  @param config   XML scraper configuration document
     *
     *  @return scraper configuration object
     *
     *  @throws ConfigurationException
     *
     */

    public static Configuration getConfig(String config)
           throws ConfigurationException
    {
        Assertion.notNull(config, "configuration filename is null");

        
        Document document = ConfigurationParser.parseDocument(config);

        return (getConfig(ConfigurationParser.parseRequest(document),
                          ConfigurationParser.parseHeaders(document),
                          ConfigurationParser.parseData(document),
                          ConfigurationParser.parseFilter(document),
                          ConfigurationParser.parseExpression(document)));
    }


    /**
     *
     *  Fabricates a scraper configuration object from an XML document,
     *  and document-specific arguments.
     *
     *  @param config       XML scraper configuration document
     *  @param arguments    document-specific arguments
     *
     *  @return scraper configuration object
     *
     *  @throws ConfigurationException
     *
     */
    
    public static Configuration getConfig(String config,
                                          String... arguments)
           throws ConfigurationException
    {
        Assertion.notNull(config, "configuration filename is null");
        Assertion.notNull(arguments, "argument array is null");
        Assertion.notNegativeNorZero(arguments.length, "bad argument count");

        ConfigurationParser parser = new ConfigurationParser(arguments);

        Document document = parser.parseDocument(config);

        return (getConfig(parser.parseRequest(document),
                          parser.parseHeaders(document),
                          parser.parseData(document),
                          parser.parseFilter(document),
                          parser.parseExpression(document)));
    }


    /**
     *
     *  Fabricates a scraper configuration object from appropriate
     *  KV Maps.
     *
     *  @param request      HTTP request request (method + URL) map
     *  @param headers      HTTP request header map
     *  @param data         HTTP request payload/data variable map
     *  @param filter       HTTP response payload filter definition map
     *  @param expression   HTTP response payload filter expression map
     *
     *  @return scraper configuration object
     *
     *  @throws ConfigurationException
     *
     */
    
    public static Configuration getConfig(Map<String, String> request,
                                          Map<String, String> headers,
                                          Map<String, String> data,
                                          Map<String, String> filter,
                                          Map<String, String> expression)
    {
        Assertion.notNull(request, "request configuration is null");
        Assertion.notNull(headers, "header configuration is null");
        Assertion.notNull(data, "data configuration is null");
        Assertion.notNull(filter, "filter configuration is null");
        Assertion.notNull(expression, "expression configuration is null");

        return (new Configuration(request, headers, data, filter, expression));
    }
}
